<?php
/**
 * 这是自定义函数
 * Enter description here ...
 * @author Administrator
 *
 */
class my_fun
{
	/**
	 * 获取指定日期所在月的第一天和最后一天
	 * Enter description here ...
	 * @param date 时间
	 */
	function GetTheMonth($date){
		$firstday = date("Y-m-01 00:00:00",strtotime($date));
		$lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
		return array($firstday,$lastday);
	}
	/**
	 * 获取指定日期所在月的第一天和最后一天
	 * Enter description here ...
	 * @param date	时间
	 */
	function TheMonth($date){
		$firstday = date("Y-m-d H:i:s",strtotime($date));
		return $firstday;
	}
	/**
	 * 获取指定日期上个月的第一天和最后一天
	 * Enter description here ...
	 * @param unknown_type $date
	 */
	function GetPurMonth($date){
		$time=strtotime($date);
		$firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
		$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
		return array($firstday,$lastday);
	}
	/**
	 * 获取指定日期下个月的第一天和最后一天
	 * Enter description here ...
	 * @param unknown_type $date
	 */
	function GetNextMonth($date){

		$arr = getdate();
		if($arr['mon'] == 12){
			$year = $arr['year'] +1;
			$month = $arr['mon'] -11;
			$day = $arr['mday'];
			if($day < 10){
				$mday = '0'.$day;
			}else {
				$mday = $day;
			}
			$firstday = $year.'-0'.$month.'-01';
			$lastday = $year.'-0'.$month.'-'.$mday;
		}else{
			$time=strtotime($date);
			$firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-01'));
			$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
		}
		return array($firstday,$lastday);
	}
	/**
	 * 发送邮件
	 * Enter description here ...
	 * @param unknown_type $news
	 * @param unknown_type $time
	 * @param unknown_type $domain
	 * @param unknown_type $username
	 * @param unknown_type $t_mail
	 */
	function sendmail($news, $time, $domain, $username, $t_mail) {
		
		import("mail/class.phpmailer.php");
		$mail = new PHPMailer ();

		$mail->IsSMTP (); // 启用SMTP
		$mail->Host = "smtp.163.com"; //SMTP服务器
		$mail->SMTPAuth = true; //开启SMTP认证
		$mail->Username = "lhlb121@163.com"; // SMTP用户名
		$mail->Password = "121956293"; // SMTP密码

		$mail->From = "lhlb121@163.com"; //发件人地址
		$mail->FromName = "监控系统"; //发件人
		$mail->AddAddress ( "$t_mail", "尊敬的客户$username您好" ); //添加收件人
		$mail->AddAddress ( "$t_mail" );
		$mail->AddReplyTo ( "$t_mail", "Information" ); //回复地址
		$mail->WordWrap = 50; //设置每行字符长度
		$mail->IsHTML ( true ); // 是否HTML格式邮件

		//$t_time=$this->formattime($time);
		$mail->Subject = "监控提示"; //邮件主题
		$mail->Body = " <b>监控提示$news</b> 故障网址：$domain  故障时间：$t_time[day]天,$t_time[hour]小时,$t_time[minute]分钟,$t_time 秒"; //邮件内容
		$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //邮件正文不支持HTML的备用显示


		if (! $mail->Send ()) {
			echo "Message could not be sent. <p>";
			echo "Mailer Error: " . $mail->ErrorInfo;
			exit ();
		}

		echo "Message has been sent";
	}
	/**
	 * 格式化时间
	 * Enter description here ...
	 * @param unknown_type $num
	 * @param unknown_type $arr
	 */
	function formattime($num,$arr=array()) {
		if($num>=86400){ //包含天 $
			$arr["day"] = sprintf('%d',($num/86400));
			$num -= $arr['day']*86400;
			if($num){
				return $this->formattime($num,$arr);
			}
			else{ 
				return $arr;
			}
		}
		elseif($num>=3600){ //小时
			$arr["hour"] = sprintf("%d",($num/3600));
			$num -= $arr['hour']*3600;
			return $this->formattime($num,$arr);
		}
		elseif($num>=60){ //分
			$arr["minute"] = sprintf("%d",($num/60));
			$arr["second"] = sprintf("%d",$num-$arr['minute']*60); 
			return $arr;
		}elseif($num<60){ 
			$arr["second"] = $num;
			return $arr;
		}
	}

	



}